<?php

namespace App\Http\Controllers\Backend;


use App\Manage\CourseManage;
use App\Models\User;
use App\Models\UserAuth;

class CourseController extends BaseController
{

    // 课程关了
    public function initialize()
    {
        parent::initialize();

        $this->model = 'Course';
    }


    // 列表
    // 添加
    // 修改
    // 删除


    // 返回列表数据
    public function lists()
    {
        $manage = new CourseManage();
        $result = $manage->onPageLists();
        if($result === false){
            return $this->error($manage->getError());
        }

        // 获取用户信息
        $uids  = array_column($result['lists'], 'uid');
        $users = (new User())->isCache(true)->getListInIds($uids);
        // 活动信息
        $eids  = array_column($result['lists'], 'auth_id');
        $auths = (new UserAuth())->isCache(true)->getListInIds($eids);
        foreach($result['lists'] as $key => $item){
            $result['lists'][$key]['user'] = $users[$item['uid']] ?? [];
            $result['lists'][$key]['auth'] = $auths[$item['uid']] ?? [];
        }

        return $this->success($result);
    }


    // 详情：修改的时候要使用的
    public function show()
    {
        $manage = new CourseManage();
        $info   = $manage->onShow();
        if($info === false){
            return $this->error($manage->getError());
        }

        // 获取用户信息
        $info['user'] = (new User())->isCache(true)->getOneById($info['uid'] ?? 0);
        $info['auth'] = (new UserAuth())->isCache(true)->getOneById($info['auth_id'] ?? 0);

        return $this->success($info);
    }

}


